package com.tao.controller;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;
import javax.persistence.criteria.CriteriaBuilder.In;

import net.sf.json.JSON;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.reflect.TypeToken;
import com.tao.entity.Brand;
import com.tao.entity.CarGood;
import com.tao.entity.DataResult;
import com.tao.entity.Product;
import com.tao.entity.ShoppingCar;
import com.tao.entity.User;
import com.tao.service.ShoppingCarService;

@Controller
@RequestMapping("/servlet/ShoppingCarServlet")

/**
 * 购物车的控制类
 * @author Administrator
 *
 */
public class ShoppingCarController {
     @Autowired 
     private ShoppingCarService shoppingCarService;
     
     //添加购物车
 	@RequestMapping(value={"/AddShoppingCar"},
 			        params={"userid","goodid","num"},
 			        method=RequestMethod.POST)
 	@ResponseBody
 	public int  AddShoppingCar(
 			@RequestParam(value="userid") int userid,
 			@RequestParam(value="goodid") int goodid,
 			@RequestParam(value="num") int num){
 		int s=0;
 		List<ShoppingCar> list=shoppingCarService.findCarByPid(goodid);
 		if(list.size()>0){
 		 s=shoppingCarService.updateShoppingCar(goodid, num); //说明该商品已经存在于购物车中，则修改数量就可以了
 		}else{
 		ShoppingCar shoppingCar=new ShoppingCar();
 		shoppingCar.setNum(num);
 		shoppingCar.setGoodid(goodid);
 		shoppingCar.setUserid(userid);
 		 s=shoppingCarService.addShoppingCar(shoppingCar); //加入购物车
 		}
 		return s;
}
 	
 	//查询购物车
 	 	@RequestMapping(value={"/findShoppingCarById"},
 	 			        params={"id"},
 	 			        method=RequestMethod.POST)
 	 	@ResponseBody
 	 	public DataResult<CarGood> findShoppingCarById(
 	 			@RequestParam(value="id") int id){
 	 		List<CarGood> list=shoppingCarService.findShoppingCarById(id);
 	 		DataResult<CarGood> dr = 
 	 				new DataResult<CarGood>(200, list, list.size());
 			  
 			return dr;
 	 		
 	}
 	 	
   //删除购物车
 	 	@RequestMapping(value={"/deleteCar"},
			        params={"carids"},
			        method=RequestMethod.POST)
	@ResponseBody
	public int  deleteCar(
			@RequestParam(value="carids") String carids){
 	 	Gson gs = new Gson();
 	 		//List<Person> jsonListObject = gs.fromJson(listStr, new TypeToken<List<Person>>(){}.getType());/
 	 	List<Integer> jsonListObject = gs.fromJson(carids, new TypeToken<List<Integer>>(){}.getType());
 	 	//String tmp = carids.substring(1,carids.length()-1);
// 	 	String[] ss = tmp.split(",");
// 	 	List<Integer> id = new ArrayList<Integer>();
// 	 	for (int i = 0; i < ss.length; i++) {
// 	 		id.add(Integer.parseInt(ss[i]));
//			System.out.println(id.get(i)+"sss");
//		}
 	 		
// 	 	JSONArray jArray = new JSONArray();
// 	 	jArray.
//// 	 	JSONArray jsonArray = JSONArray.fromObject(carids);
// 	 	
// 	 	
// 	 	List<Integer> tmp = JSONArray.toList(jsonArray);
// 	 	if (tmp.size()==0) {
// 	 		return 0;
// 	 	}
		int s=shoppingCarService.deleteCar(jsonListObject); 
		return s;
    }
 	 	
 	/*public void deleteCar1(List<Integer> tmp) {
 		
 	}*/
 	 	
 	public class TMP {
 		
 		public List<Integer> data;

		public List<Integer> getData() {
			return data;
		}

		public void setData(List<Integer> data) {
			this.data = data;
		}
 		
 	}
}


